import 'dart:async'; //做异步
import 'package:dio/dio.dart';
import 'package:hairen_app/server/saveInfo.dart';
import 'package:fluttertoast/fluttertoast.dart';
class Server {

  static Dio request([String token=""]){

    Dio dio=new Dio();
    dio.interceptor.request.onSend = (Options options) {
      options.baseUrl="http://wmoves.xyz";
//      print('send request：path:${options.path}，baseURL:${options.baseUrl}');
      options.headers["XX-Token"] = token;
      options.headers["XX-Device-Type"] = "android";
      return options;
    };
    dio.interceptor.response.onSuccess = (Response response) {
     if(response.data['code']==10001){
       SaveInfo.clearToken();
       SaveInfo.clearUserInfo();
     }
      return response; // continue
    };
    return dio;
  }

  //获取版本
  static Future getVersion({Map<String,String> data}) async {
    Response  response =await request().get('/public/api/home/index/getVersion');
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //banner
  static Future getBanner({Map<String,String> data}) async {
   try{
     Response  response =await request().get('/public/api/home/Slides/1');
     if(response.statusCode==200){
       return response.data;
     }else{
       throw Exception("Faild to load categories");
     }
   } on DioError catch(e){
     print("错误：${e}");
   }

  }

  //最新
  static Future getUpTime({Map<String,String> data}) async {
    Response  response =await request().get('/public/api/home/Moves/upTime');
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //热门
  static Future getHot({Map<String,String> data}) async {
    Response  response =await request().get('/public/api/home/Moves/upHot');
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //热门
  static Future getRecommend({Map<String,String> data}) async {
    Response  response =await request().get('/public/api/home/Moves/upRecommend');
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //获取电影详情
  static Future getMove({Map<String,String> data}) async{

    Response  response =await request().post('/public/api/home/Moves/move',data: data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //获取电影
  static Future getMovesRows({Map<String,String> data})async{

    var url='/public/api/home/Moves/index/';
    Response response= await request().post(url,data:data);

    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }


  //获取搜索热门
  static Future getUpSearch({Map<String,String> data})async{
    var url='/public/api/home/Moves/upSearch/';
    Response response= await request().post(url,data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //搜索电影
  static Future getSearchMoves({Map<String,String> data})async{
    var url='/public/api/home/Moves/search/';
    Response response= await request().post(url,data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //记录搜索次数
  static Future updateHot({Map<String,String> data})async{
    var url='/public/api/home/Moves/updateHot/';
    Response response= await request().post(url,data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //注册账号
  static Future register({Map<String,String> data})async{
    var url='/public/api/user/public/register';
    Response response= await request().post(url,data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //登陆账号
  static Future login({Map<String,String> data})async{
    var url='/public/api/user/public/login';
    Response response= await  request().post(url,data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  // 获取收藏夹 /public/api/user/Move_favorite/getFavorite
 static Future getFavorite({Map<String,String> data})async {
  String token=await  SaveInfo.getToken();
   var url='/public/api/user/Move_favorite/getFavorite';
   Response response= await request(token).post(url,data:data);
   if(response.statusCode==200){
     return response.data;
   }else{
     throw Exception("Faild to load categories");
   }
 }

 //添加收藏夹
  static Future addFavorite({Map<String,String> data})async {
    String token=await  SaveInfo.getToken();
    var url='/public/api/user/Move_favorite/addFavorite';
    Response response= await request(token).post(url,data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }
  //编辑收藏夹
  static Future updateFavorite({Map<String,String> data})async {
    String token=await  SaveInfo.getToken();
    var url='/public/api/user/Move_favorite/updateFavorite';
    Response response= await request(token).post(url,data:data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //删除收藏夹
  static Future delFavorite({Map<String,String> data})async {
    String token=await  SaveInfo.getToken();
    var url='/public/api/user/Move_favorite/delFavorite';
    Response response= await request(token).post(url,data:data);
    print(response.data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  //视频添加到收藏夹

  static Future setFavorite({Map<String,String> data})async {
    String token=await  SaveInfo.getToken();
    var url='/public/api/user/Move_favorite/setFavorite';
    Response response= await request(token).post(url,data:data);
    print(response.data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }

  static Future isFavorite({Map<String,String> data})async {
    String token=await  SaveInfo.getToken();
    var url='/public/api/user/Move_favorite/isFavorite';
    Response response= await request(token).post(url,data:data);
    print(response.data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }
//  取消收藏 cancelFavorite
  static Future cancelFavorite({Map<String,String> data})async {
    String token=await  SaveInfo.getToken();
    var url='/public/api/user/Move_favorite/cancelFavorite';
    Response response= await request(token).post(url,data:data);
    print(response.data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }
  // 获取所有收藏夹电影 getFavoriteList
  static Future getFavoriteList({Map<String,String> data})async {
    String token=await  SaveInfo.getToken();
    var url='/public/api/user/Move_favorite/getFavoriteList';
    Response response= await request(token).post(url,data:data);
    print(response.data);
    if(response.statusCode==200){
      return response.data;
    }else{
      throw Exception("Faild to load categories");
    }
  }
}
